$(function () {
    $.idcode.setCode();
    $.validator.addMethod("isCheckeUname", (value, element, param) => {
        var reg = /^[a-z]\w{5,11}$/i
        return reg.test(value); 
    })
    $.validator.addMethod("isCheckeUpwd", (value, element, param) => {
        var reg = /^\w{6,12}$/i
        return reg.test(value); 
    })
    $("form").validate({
        rules: {
            uname: {
                required: true, 
                rangelength: [6, 12],
                isCheckeUname: true, 
                remote: "http://127.0.0.1:8080/api/checkUname"
            },
            upwd: {
                required: true, 
                rangelength: [6, 12],
                isCheckeUpwd: true,
            },
            upwd2: {
                equalTo: "#u_pwd"
            },
            yzm: {
                required: true,
            }
        },
       
        messages: {
            uname: {
                required: "用户名必填",
                rangelength: "用户名的长度必须{0}-{1}",
                isCheckeUname: "用户名不能以数字开头",
                remote: "该用户已注册!"
            },
            upwd: {
                required: "密码不能为空",
                rangelength: "密码的长度必须{0}-{1}",
                isCheckeUpwd: '密码不合法'
            },
            upwd2: {
                equalTo: "2次密码不一致"
            },
            yzm: {
                required: "验证码必填",
            }
        },
        
        submitHandler: function () {
            var IsBy = $.idcode.validateCode();
            if (IsBy == true) {


                $.ajax({
                    url: "./api/reg",
                    type: "post",
                    data: $("form").serialize(),
                }).then((result) => {
                    if (result.status == 1) {
                        // alert("注册成功")
                    }
                    layer.msg(result.msg);
                    window.location.href="./login.html"
                })

            }

            return false;
        }
    })
    layer.confirm("内容提示", {
        btn: ["yes", "no"],
        icon: 1,
        skin: 'layer-ext-moon'
    }, function (index) {
        alert("yes");
        layer.close(index);
    }, function () {
        alert("no");
    })
})